package services

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/dao"
	"hft/global"
	"hft/tools"
	"regexp"
)

type Room struct{}

func (*Room) CreateRoom(claims tools.BaseClaims, data *admin.Room) (err error) {

	var account *admin.Account

	var louPan *admin.LouPan

	var roomType *admin.RoomType

	var sellStatus *admin.SellStatus

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("找不到账号信息")
	}

	err = global.OC_DB.First(&louPan, data.LouPanID).Error

	if err != nil {
		return errors.New("找不到楼盘信息")
	}

	match, err := regexp.MatchString(fmt.Sprintf("(%v)", account.RegionLink), louPan.RegionLink)

	if err != nil {
		return errors.New("未知错误500")
	}

	if !match {
		return errors.New("你无法在该楼盘中添加户型")
	}

	err = global.OC_DB.First(&roomType, data.RoomId).Error

	if err != nil {
		return errors.New("无效的户型")
	}

	err = global.OC_DB.First(&sellStatus, data.Status).Error

	if err != nil {
		return errors.New("无效的销售状态")
	}

	// data.LouPanName = louPan.LouPanName.Name

	err = global.OC_DB.Create(&data).Error

	if err != nil {
		return errors.New("未知错误500")
	}

	return nil
}

func (*Room) EditRoom(claims tools.BaseClaims, data *admin.Room) (err error) {
	var account *admin.Account

	var louPan *admin.LouPan

	var room *admin.Room

	if data.ID == 0 {
		return errors.New("无效的户型")
	}

	err = global.OC_DB.First(&room, data.ID).Error

	if err != nil {
		return errors.New("找不到该户型")
	}

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("找不到账号信息")
	}

	err = global.OC_DB.First(&louPan, data.LouPanID).Error

	if err != nil {
		return errors.New("找不到楼盘信息")
	}

	match, err := regexp.MatchString(fmt.Sprintf("(%v)", account.RegionLink), louPan.RegionLink)

	if err != nil {
		return errors.New("未知错误500")
	}

	if !match {
		return errors.New("你无法在该楼盘中添加户型")
	}

	if data.LouPanID != int(louPan.ID) {
		return errors.New("该户型不属于该楼盘")
	}
	global.OC_DB.Model(&admin.Room{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
		"lou_pan_name":  data.Name,
		"name":          data.Name,
		"area":          data.Area,
		"room_id":       data.RoomId,
		"office":        data.Office,
		"bath_room":     data.BathRoom,
		"sort":          data.Sort,
		"first_pay":     data.FirstPay,
		"grounding":     data.Grounding,
		"forward":       data.Forward,
		"property_use":  data.PropertyUse,
		"status":        data.Status,
		"cover":         data.Cover,
		"room_pay_type": data.RoomPayType,
	})

	return nil
}

func (*Room) GetRoomList(louPanID int, claims tools.BaseClaims) (data []*admin.Room, err error) {

	var account *admin.Account

	var louPan *admin.LouPan

	var rooms []*admin.Room

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return nil, errors.New("找不到账号信息")
	}

	err = global.OC_DB.First(&louPan, louPanID).Error

	if err != nil {
		return nil, errors.New("找不到楼盘信息")
	}

	match, err := regexp.MatchString(fmt.Sprintf("(%v)", account.RegionLink), louPan.RegionLink)

	if err != nil {
		return nil, errors.New("未知错误500")
	}

	if !match {
		return nil, errors.New("你无法获取该楼盘户型")
	}

	global.OC_DB.Where("lou_pan_id", louPanID).Preload("SellStatus").Preload("RoomType").Order("sort desc").Find(&rooms)
	for _, v := range rooms {
		v.Cover = global.OC_CONFIG.AliyunOSS.BucketUrl + v.Cover
	}

	return rooms, nil
}

func (*Room) GetRoomInfo(claims tools.BaseClaims, roomId int) (room *admin.Room, err error) {

	var account *admin.Account

	var roomInfo *admin.Room

	var louPan *admin.LouPan

	if roomId == 0 {
		return nil, errors.New("无效的户型")
	}

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return nil, errors.New("找不到账号信息")
	}

	err = global.OC_DB.Preload("RoomType").Preload("SellStatus").First(&roomInfo, roomId).Error

	if err != nil {
		return nil, errors.New("找不到该户型")
	}

	err = global.OC_DB.First(&louPan, roomInfo.LouPanID).Error

	if err != nil {
		return nil, errors.New("找不到楼盘信息")
	}

	match, err := regexp.MatchString(fmt.Sprintf("(%v)", account.RegionLink), louPan.RegionLink)

	if err != nil {
		return nil, errors.New("未知错误500")
	}

	if !match {
		return nil, errors.New("你无法获取该户型信息")
	}

	return roomInfo, nil
}

var RoomService = new(Room)
